﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.GUI.Common;
using DblogStudio.GUI.Utils;
using DblogStudio.Utils;
namespace DblogStudio.GUI.HeThong
{
    public partial class FormCauHinhHeThong : BaseDialog
    {
        public FormCauHinhHeThong()
        {
            InitializeComponent();
            using (var db = DAL.Models.CEMS4UG_DHKHContext.GetContext())
            {
                var dsCBDesktop = BizAppConfig.GetDSCanhBaoDesktop(db);
                var dsCBMobile = BizAppConfig.GetDSCanhBaoMobile(db);
                if (dsCBDesktop == "0")
                    rdToiHan.Checked = true;
                else
                    rdTruocHan.Checked = true;
                int temp = 0;
                if (int.TryParse(dsCBDesktop, out temp))
                    numDesktop.Value = int.Parse(dsCBDesktop);
                else
                    numDesktop.Value = 0;

                if (int.TryParse(dsCBMobile, out temp))
                    numMobile.Value = int.Parse(dsCBMobile);
                else
                    numMobile.Value = 0;
                
                var mauTruocHan = BizAppConfig.GetMAUTRUOCHAN(db);
                if (int.TryParse(mauTruocHan, out temp))
                    lbTruocHan.BackColor = Color.FromArgb(int.Parse(mauTruocHan));
                else
                    lbTruocHan.BackColor = Color.Green;
                
                var mauToiHan = BizAppConfig.GetMAUTOIHAN(db);
                if (int.TryParse(mauToiHan, out temp))
                    lbDenHan.BackColor = Color.FromArgb(int.Parse(mauToiHan));
                else
                    lbDenHan.BackColor = Color.Red;

                var mauQuaHan30 = BizAppConfig.GetMAUQUAHANDUOI30(db);
                if (int.TryParse(mauQuaHan30, out temp))
                    lbQuaHanDuoi30.BackColor = Color.FromArgb(int.Parse(mauQuaHan30));
                else
                    lbQuaHanDuoi30.BackColor = Color.Red;

                var mauQuaHan = BizAppConfig.GetMAUQUAHAN(db);
                if (int.TryParse(mauQuaHan, out temp))
                    lbQuaHan.BackColor = Color.FromArgb(int.Parse(mauQuaHan));
                else
                    lbQuaHan.BackColor = Color.Black;

                txtTenCongTy.Text = BizAppConfig.GetTENCONGTY();
                txtDiaChi.Text = BizAppConfig.GetDIACHICT();
                if (!string.IsNullOrEmpty(BizAppConfig.GetLOGOCONGTY()))
                    LoadImage();

            }
        }

        private void bntCapNhat_Click(object sender, EventArgs e)
        {
            #region Thu thập dữ liệu
            thongBao.Clear();
            #endregion

            try
            {
                var truocHanDesktop = "0";
                var truocHanMobile = "0";
                if (rdTruocHan.Checked)
                {
                    truocHanDesktop = ((int)numDesktop.Value).ToString();
                    truocHanMobile = ((int)numMobile.Value).ToString();
                }
                var picStr = "";
                //if (picLogo.Image != null)
                //    picStr = ConvertImageToString(picLogo.Image);
                BizAppConfig.SaveAppConfig(truocHanDesktop,
                    truocHanMobile,
                    lbQuaHan.BackColor.ToArgb().ToString(),
                    lbQuaHanDuoi30.BackColor.ToArgb().ToString(),
                    lbDenHan.BackColor.ToArgb().ToString(),
                    lbTruocHan.BackColor.ToArgb().ToString(),
                    txtTenCongTy.Text,
                    txtDiaChi.Text,
                    picStr
                    );
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
                //LoadImage(picStr);
            }
            catch (Exception exc)
            {
                DialogUtils.ShowErrorWithDefaultTitle("Không cập nhật thông tin hệ thống được.\r\n", exc.Message);
            }
        }

        private void lbTruocHan_Click(object sender, EventArgs e)
        {
            ColorDialog colorDig = new ColorDialog();
            if (colorDig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lbTruocHan.BackColor = colorDig.Color;
            }
        }

        private void rdTruocHan_CheckedChanged(object sender, EventArgs e)
        {
            numMobile.Enabled = numDesktop.Enabled = rdTruocHan.Checked;

        }

        private void lbDenHan_Click(object sender, EventArgs e)
        {
            ColorDialog colorDig = new ColorDialog();
            if (colorDig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lbDenHan.BackColor = colorDig.Color;
            }
        }

        private void lbQuaHan_Click(object sender, EventArgs e)
        {
            ColorDialog colorDig = new ColorDialog();
            if (colorDig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lbQuaHan.BackColor = colorDig.Color;
            }
        }

        private void picLogo_Click(object sender, EventArgs e)
        {
            //OpenFileDialog op = new OpenFileDialog();
            //op.Filter = "File ảnh (*.png) | *.png";
            //if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    picLogo.Load(op.FileName);
            //}
        }
        string ConvertImageToString(Image image)
        {
            //Image img = picturebox1.Image();
            byte[] arr;
            ImageConverter converter = new ImageConverter();
            arr = (byte[])converter.ConvertTo(image, typeof(byte[]));
            return Encoding.UTF8.GetString(arr, 0, arr.Length);
        }
        byte[] ConvertStringToImage(string str)
        {
            return Encoding.UTF8.GetBytes(str);
        }
        string GetImageFromArray(byte[] arr)
        {
            FileStream fs = new FileStream("logo.png", FileMode.Create);
            fs.Write(arr, 0, arr.Length);
            fs.Close();
            fs = null;
            return "logo.png";
        }
        void LoadImage()
        {
            var str = BizAppConfig.GetLOGOCONGTY();
            if (!string.IsNullOrEmpty(str))
            {
                var byteArray = ConvertStringToImage(str);
                string imagePath = GetImageFromArray(byteArray);
                //picLogo.Image = Image.FromFile(imagePath);
                //picLogo.Refresh();
            }
        }

        private void lbQuaHanDuoi30_Click(object sender, EventArgs e)
        {
            ColorDialog colorDig = new ColorDialog();
            if (colorDig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lbQuaHanDuoi30.BackColor = colorDig.Color;
            }
        }
        //void LoadImage(string str)
        //{
        //    if (!string.IsNullOrEmpty(str))
        //    {
        //        var byteArray = ConvertStringToImage(str);
        //        string imagePath = GetImageFromArray(byteArray);
        //        pictureBox1.Image = Image.FromFile(imagePath);
        //        pictureBox1.Refresh();
        //    }
        //}
    }
}
